﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dracham.Classes
{
    public class Position : ICloneable
    {
        public float X { get; set; }
        public float Y { get; set; }
        public string ZoneId { get; set; }

        public Position() { }
        public Position(float x, float y, string zoneId)
        {
            this.X = x;
            this.Y = y;
            this.ZoneId = zoneId;
        }

        public Zone Zone
        {
            get
            {
                return GameWorld.Zones.SingleOrDefault(z => z.ZoneId == this.ZoneId);
            }
        }

        public double Distance(Object o)
        {
            return this.Distance(o.Position);
        }

        public double Distance(Position p)
        {
            if (p.ZoneId == this.ZoneId)
            {
                return this.Distance(p.X, p.Y);
            }

            return double.NaN;
        }

        public double Distance(float x, float y)
        {
            if (this.X == x && this.Y == y) return 0;

            float deltaX = x - this.X;
            float deltaY = y - this.Y;

            double hypotenuse = Convert.ToDouble(deltaX * deltaX + deltaY * deltaY);

            return Math.Sqrt(hypotenuse);
        }

        public Boolean IsEqual(Position pos)
        {
            if (pos == null) return false;

            if (this.X == pos.X &&
                this.Y == pos.Y &&
                this.ZoneId == pos.ZoneId) return true;

            return false;
        }

        public NetworkParameter[] GetNetworkDescription()
        {
            List<NetworkParameter> netParams = new List<NetworkParameter>();
            netParams.Add(new NetworkParameter(this.X.ToString(), 5));
            netParams.Add(new NetworkParameter(this.Y.ToString(), 5));
            return netParams.ToArray();
        }

        public override string ToString()
        {
            string zone = this.Zone == null ? "Id: " + this.ZoneId : "Name: " + this.Zone.Name;
            return String.Format("{0},{1} ({2})", this.X, this.Y, zone);
        }

        #region ICloneable Members

        public object Clone()
        {
            return new Position(this.X, this.Y, this.ZoneId);
        }

        #endregion
    }
}
